// pages/find/detail/detail.js
const T = getApp().globalData.T
const config = getApp().globalData.config
import {collect, isCollect} from '../../../utils/collect.js'
import { $wuxDialog } from '../../../wux-components/index'
import { shareUrl } from '../../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowPopup: false,
    id: 0,
    commentid: -1,
    lord: '',
    index: '',
    subindex: '',
    userid: -1,
    commentid: -1,
    token: '',
    userId: '',
    replyList: [],
    collectStatus: 0,
    isAuthor: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {
      id
    } = options;
    this.setData({ id: id })
    this.data.token = getApp().globalData.token;
    this.data.userId = getApp().globalData.userId;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPostDetail();
    this.getPostComment();
    this.getCollectStatus();
  },

  toast(text, type) {
    T.toast({
      id: '#wux-toast',
      text: text,
      success: () => { }
    })
  },

  getCollectStatus() {
    isCollect(this.data.id, 2).then(res => {
      this.setData({
        collectStatus: res
      })
    })
  },

  getPostDetail() {
    T.req({
      url: config.api + 'forum/topic/' + this.data.id,
      method: 'GET',
      data: {
        token: this.data.token,
        userId: this.data.userId,
      },
      success: (res) => {
        let posterInfo = {
          title: res.title,
          username: res.username,
          headImg: res.headImg,
          time: res.time,
          replySum: res.replySum,
          userType: res.userType
        }
        this.setData({
          posterInfo,
          isAuthor: res.isAuthor,
          content: res.content,
          photos: res.photos,
          activity: {
            id: res.actId,
            activity: res.activity,
            actCount: res.actCount
          }
        });
      }
    });
  },

  onActivityDetail(e) {
    let actid = e.currentTarget.dataset.actid;
    let topicid = e.currentTarget.dataset.topicid;
    if (this.data.isAuthor == 1) {
      wx.navigateTo({
        url: '../activity/detail?id=' + topicid,
      })
      return;
    }
    wx.navigateTo({
      url: '../activity/join?id=' + actid,
    })

  },

  getPostComment() {
    T.req({
      url: config.api + 'forum/topic/comment/list',
      method: 'GET',
      data: {
        token: this.data.token,
        topicId: this.data.id,
        userId: this.data.userId
      },
      success: (res) => {
        this.setData({
          replyList: res.list,
        });
      }
    })
  },

  onDelComment(e) {
    let {
      lord,
      index,
      subindex,
      iscommentator,
      commentid
    } = e.detail;
    console.log(e.detail);
    if (lord === 'true') {
      // 楼主
      if (iscommentator) {
        this.setData({
          isShowPopup: true,
          lord,
          index,
          commentid,
          iscommentator
        });
      }
    } else {
      // 层主
      if (iscommentator) {
        this.setData({
          isShowPopup: true,
          lord,
          subindex,
          commentid,
          iscommentator
        });
      }
    }
  },

  /**
   *回复评论 
   */
  onReply(e) {
    console.log(e);
    let _param = {
      topicId: this.data.id,
      commentId: e.detail.commentid,
      lordid: e.detail.lordid,
      lord: 0
    }
    wx.navigateTo({
      url: '../add-comment/comment?' + T.param(_param),
    })
  },

  onClose() {
    this.setData({
      isShowPopup: false
    })
  },

  onDelPost() {
    $wuxDialog().confirm({
      resetOnClose: true,
      closable: true,
      title: '提示',
      content: '是否删除该贴子',
      onConfirm: (e) => {
        T.req({
          loadTitle: '请求中...',
          url: config.api + 'forum/topic/' + this.data.id + '?token=' + this.data.token,
          method: 'DELETE',
          success: (res) => {
            T.toast({
              id: '#wux-toast', text: '操作成功', success: () => {
                wx.navigateBack({
                  delta: 1
                })
              }
            });
          }
        })
      },
      onCancel(e) {

      },
    })
  },

  onSureDel() {
    let {
      lord,
      index,
      subindex,
      commentid,
      iscommentator
    } = this.data;
    console.log(this.data)
    if (lord === 'true') {
      // 楼主
      if (iscommentator) {
        // todo 调用删除评价接口
        T.req({
          url: config.api + 'forum/topic/comment/' + commentid + '?token=' + this.data.token,
          method: 'DELETE',
          data: {},
          success: (res) => {
            // 自己的评论
            // 删除评论
            this.data.replyList.splice(index - 1, 1);
            this.setData({
              isShowPopup: false,
              replyList: this.data.replyList
            });
            this.toast('删除成功');
          }
        })
      }
    } else {
      // 层主
      if (iscommentator) {
        // 自己的评论
        // 第一级数据位置
        let _index = subindex.split('-')[0] - 1;
        // 第二级数据位置
        let _subindex = subindex.split('-')[1];
        // todo 调用删除评价接口
        T.req({
          url: config.api + 'forum/topic/comment/' + commentid + '?token=' + this.data.token,
          method: 'DELETE',
          data: {
            token: this.data.token
          },
          success: (res) => {
            // 删除评论
            this.data.replyList[_index].replyCommentVos.splice(_subindex, 1);
            this.setData({
              isShowPopup: false,
              replyList: this.data.replyList
            });
            this.toast('删除成功');
          }
        })
      }
    }
  },

  onCollectPos() {
    let _param = {
      token: this.data.token,
      userId: this.data.userId,
      type: 2,
      relatedId: this.data.id
    }
    collect(_param).then((res) => {
      T.toast({
        id: '#wux-toast',
        text: this.data.collectStatus ? '取消成功' : '收藏成功'
      })
      this.setData({
        collectStatus: 1 ^ this.data.collectStatus
      })
    })
  },

  /**
   * 发评论
   */
  onPublishComment() {
    let _param = {
      topicId: this.data.id,
      commentId: 0,
      toUserId: -1,
      lord: 1,
    }
    wx.navigateTo({
      url: '../add-comment/comment?' + T.param(_param),
    });
  },

  onShareAppMessage() {
    let _path = shareUrl({
      id: this.data.id
    });

    return {
      title: "帖子详情",
      path: _path,
    }
  }
})